package example;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.curator.framework.recipes.locks.PredicateResults;
import org.apache.curator.framework.recipes.locks.StandardLockInternalsDriver;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.Arrays;
import java.util.List;

public class DistributedLock {

    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
        client.start();

        // 可重入锁
        /*InterProcessMutex interProcessMutex = new InterProcessMutex(client, "/locks");
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]尝试获取锁");
                        interProcessMutex.acquire();
                        System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]成功获取到了锁，开始工作");
                        Thread.sleep(30000);
                        interProcessMutex.release();
                        System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]释放锁");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }).start();
        }*/

        // 信号量
        /*InterProcessSemaphoreV2 semaphores = new InterProcessSemaphoreV2(client, "/semaphores", 2);
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try {
                    System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]尝试获取锁");
                    Lease lease = semaphores.acquire();
                    System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]成功获取到了锁，开始工作");
                    Thread.sleep(300000);
                    semaphores.returnLease(lease);
                    System.out.println(new Date() + "：线程[" + Thread.currentThread().getName() + "]释放锁");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }*/

        // 不可重入锁
        /*InterProcessSemaphoreMutex semaphoreMutex = new InterProcessSemaphoreMutex(client, "/semaphore_mutex");
        semaphoreMutex.acquire();
        boolean acquiredInThisProcess = semaphoreMutex.isAcquiredInThisProcess();
        System.out.println("是否已获得锁: " + acquiredInThisProcess);
        semaphoreMutex.release();
        semaphoreMutex.acquire();
        semaphoreMutex.acquire();
        System.out.println("锁重入成功");
        semaphoreMutex.release();*/

        // 读写锁
        InterProcessReadWriteLock readWriteLock = new InterProcessReadWriteLock(client, "/read_write_lock");
        InterProcessMutex readLock = readWriteLock.readLock();
        InterProcessMutex writeLock = readWriteLock.writeLock();
        readLock.acquire();
        writeLock.acquire();
        System.out.println("------------成功------------");
    }
}